//
//  SQRectificationDetailsViewController.m
//  energy
//
//  Created by 周曼迪 on 2021/7/12.
//

#import "SQRectificationDetailsViewController.h"

#import "SQQueryModel.h"
#import "SQRectificationDetailsModel.h"

#import "LCImageChoose.h"
#import "SQShowImage.h"

#import "NSString+NCDate.h"
#import "SQPlayVoiceView.h"
#import "SQVoiceView.h"



@interface SQRectificationDetailsViewController ()<BaseTableVCInterface>
/**录音*/
@property (strong, nonatomic) SQVoiceView *voiceView;
/**播放录音*/
@property (strong, nonatomic) SQPlayVoiceView * playVoiceView;
@property (strong, nonatomic) SQPlayVoiceView * playVoiceLookView;
@property (weak, nonatomic) IBOutlet UIView *view_confirm;
@property (weak, nonatomic) IBOutlet UIButton *btn_confirm;
/**隐患信息*/
@property (weak, nonatomic) IBOutlet UITextField *tf_type;//隐患类别
@property (weak, nonatomic) IBOutlet UITextField *tf_level;//隐患等级
@property (weak, nonatomic) IBOutlet UITextField *tf_rectificationType;//隐患整改类型
@property (weak, nonatomic) IBOutlet UITextField *tf_completionTime;//计划完成时间
@property (weak, nonatomic) IBOutlet UITextField *tf_money;//整改资金
@property (weak, nonatomic) IBOutlet UITextField *tf_prevention;//防控措施
@property (weak, nonatomic) IBOutlet UITextField *tf_system;//制度措施清单
@property (weak, nonatomic) IBOutlet UITextField *tf_rectification;//整改措施
@property (weak, nonatomic) IBOutlet UITextView *tv_note;//备注
@property (weak, nonatomic) IBOutlet UIView *view_information;
@property (weak, nonatomic) IBOutlet UIButton *btn_information;
@property (weak, nonatomic) IBOutlet UITextField *tf_area;//区域
@property (weak, nonatomic) IBOutlet UITextField *tf_people;//发现人
@property (weak, nonatomic) IBOutlet UITextField *tf_department;//部门
@property (weak, nonatomic) IBOutlet UITextField *tf_assessmentTime;//评估时间
@property (weak, nonatomic) IBOutlet UITextView *tv_describe;//隐患描述
/**图片三张*/
@property (weak, nonatomic) IBOutlet UIImageView *img_picOne;
@property (weak, nonatomic) IBOutlet UIImageView *img_picTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_picThree;

/**提交按钮*/
@property (weak, nonatomic) IBOutlet UIButton *btn_submit;

@property (strong, nonatomic) SQQueryModel *queryModel;

/**整改上传三张图 删除图*/
@property (weak, nonatomic) IBOutlet UIImageView *img_confirmOne;
@property (weak, nonatomic) IBOutlet UIImageView *img_confirmOneDel;
@property (weak, nonatomic) IBOutlet UIImageView *img_confirmTwo;
@property (weak, nonatomic) IBOutlet UIImageView *img_confirmTwoDel;
@property (weak, nonatomic) IBOutlet UIImageView *img_confirmThree;
@property (weak, nonatomic) IBOutlet UIImageView *img_confirmThreeDel;
/**隐患整改照片 三张*/
@property (strong, nonatomic) NSString *hd_report_pic_one;
@property (strong, nonatomic) NSString *hd_report_pic_two;
@property (strong, nonatomic) NSString *hd_report_pic_three;
@property (copy, nonatomic) NSString *hd_rectification_pic_audio;

@property (weak, nonatomic) IBOutlet UIButton *btn_one;
@property (weak, nonatomic) IBOutlet UIButton *btn_two;
@property (weak, nonatomic) IBOutlet UIButton *btn_three;

@property (weak, nonatomic) IBOutlet UIView *view_voice;

@property (weak, nonatomic) IBOutlet UIView *view_voiceConfirm;



@end

@implementation SQRectificationDetailsViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    
    self.view_confirm.hidden = NO;
    self.view_information.hidden = YES;
    
    self.navigationItem.title = @"隐患整改";
    
    self.hd_report_pic_one = @"";
    self.hd_report_pic_two = @"";
    self.hd_report_pic_three = @"";
    self.hd_rectification_pic_audio = @"";
    
    self.playVoiceView = [[SQPlayVoiceView alloc] init];
    [self.view_voiceConfirm addSubview:self.playVoiceView];
    
    if ([self.type isEqualToString:@"1"]) {//查看
        self.btn_submit.hidden = YES;
        self.btn_one.hidden = YES;
        self.btn_two.hidden = YES;
        self.btn_three.hidden = YES;
        self.tv_note.editable = NO;
        self.playVoiceLookView = [[SQPlayVoiceView alloc] init];
        [self.view_voice addSubview:self.playVoiceLookView];
    }else if ([self.type isEqualToString:@"2"]){//整改
        
        self.btn_submit.hidden = NO;
        self.btn_one.hidden = NO;
        self.btn_two.hidden = NO;
        self.btn_three.hidden = NO;
        self.tv_note.editable = YES;
        
        self.voiceView = [[SQVoiceView alloc]init];
        [self.voiceView showWithChooseBlock:^(NSString * _Nonnull path) {
            self.hd_rectification_pic_audio = path;

        }];
        [self.view_voice  addSubview:self.voiceView];

    }

    [self getQueryData];

    
}


-(void)getQueryData{
    [SQQueryModel getDataWithParams:@{@"hd_id":self.hd_id} WithCallBack:^(SQQueryModel * data) {
        self.queryModel = data;
        
        self.tf_area.text = data.data.site_area;
        self.tf_type.text = data.data.hd_category_text;
        self.tf_level.text = data.data.hd_level_text;
        self.tf_people.text = data.data.hd_report_person_text;
        self.tf_rectification.text = data.data.hd_list_of_system_measures;
        self.tf_assessmentTime.text = [NSString formateDate:[data.data.hd_report_time substringWithRange:NSMakeRange(6,13)]];
        self.tf_department.text = data.data.hd_report_person_text;
        self.tf_rectificationType.text = data.data.hd_rectification_type_text;
        self.tf_completionTime.text = data.data.hd_plan_complete;
        self.tf_money.text = data.data.hd_rectification_funds_text;
        self.tf_system.text = data.data.hd_list_of_system_measures;
        self.tf_prevention.text = data.data.hd_prevention_and_control_measures;
        self.tf_rectification.text = data.data.hd_rectification_measures;
        self.tv_describe.text = data.data.hd_danger_description;

        [self.playVoiceView buildPlayVoiceVie:data.data.hd_report_pic_audio];

        
        [self.img_picOne setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_one]] placeholder:[UIImage imageNamed:@"addImage"]];
    
        [self.img_picTwo setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_two]] placeholder:[UIImage imageNamed:@"addImage"]];
    
        [self.img_picThree setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_ReqHeader,data.data.hd_report_pic_three]] placeholder:[UIImage imageNamed:@"addImage"]];
        
    }];
}



//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

-(NSInteger)rowsCount:(NSInteger)section{
    return 0;
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQConfirmTableViewCell"]];
}


//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

- (IBAction)clickPicOne:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_report_pic_one]]) {
        [SQShowImage showImage:self.img_confirmOne];
        
    }else{
    
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_confirmOne.image = data[0];
            self.hd_report_pic_one =   [UIImageJPEGRepresentation(self.img_confirmOne.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
          
            self.img_confirmOneDel.image = [UIImage imageNamed:@"del"];
        }];
    }
   
}

- (IBAction)clickDelPicOne:(UIButton *)sender {
    self.img_confirmOne.image = [UIImage imageNamed:@"addImage"];
   self.img_confirmOneDel.image = [UIImage imageNamed:@""];
    self.hd_report_pic_one = @"";
}


- (IBAction)clickPicTwo:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_report_pic_two]]) {
        [SQShowImage showImage:self.img_confirmTwo];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_confirmTwo.image = data[0];
            self.hd_report_pic_two = [UIImageJPEGRepresentation(self.img_confirmTwo.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            
            self.img_confirmTwoDel.image = [UIImage imageNamed:@"del"];
        }];
    }
 
   
}

- (IBAction)clickDelPicTwo:(UIButton *)sender {
    self.img_confirmTwoDel.image = [UIImage imageNamed:@""];
    self.img_confirmTwo.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_two = @"";
   
}


- (IBAction)clickPicThree:(UIButton *)sender {
    [self.view endEditing:YES];
    if ([self haveStr:@[self.hd_report_pic_three]]) {
        [SQShowImage showImage:self.img_confirmThree];
        
    }else{
        [LCImageChoose shareImageWithKey:@"" withImageMax:1 withCallBack:^(NSArray *data) {
            self.img_confirmThree.image = data[0];
            self.hd_report_pic_three = [UIImageJPEGRepresentation(self.img_confirmThree.image, 0.5f) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
            self.img_confirmThreeDel.image = [UIImage imageNamed:@"del"];
        }];
    }
 
   
}

- (IBAction)clickDelPicThree:(UIButton *)sender {
    self.img_confirmThreeDel.image = [UIImage imageNamed:@""];
   self.img_confirmThree.image = [UIImage imageNamed:@"addImage"];
    self.hd_report_pic_three = @"";
}

- (IBAction)clickSubmit:(UIButton *)sender {
    /**方法用途：隐患整改操作。传参：
     隐患id(hd_id),
     隐患整改人id(hd_rectification_person_id),
     隐患整改人中文(hd_rectification_person_text),
     隐患整改人单位id(hd_rectification_company_id),
     隐患整改人单位中文(hd_rectification_company_text),
     隐患整改第一张照片(hd_rectification_pic_one),第二张照片(hd_rectification_pic_two),第三张照片(hd_rectification_pic_three),隐患整改音频(hd_rectification_pic_audio),整改情况描述(hd_description_of_rectification)。成功回参：code：200|msg。失败回参：code：0|msg。*/
   
    if ([self verification]) {
        [SQRectificationDetailsModel getDataWithParams:@{@"hd_id":self.hd_id,
                                                         
                                                         @"hd_rectification_person_id":UID,
                                                         @"hd_rectification_person_text":MName,
                                                         
                                                         @"hd_rectification_company_id":MGroup,
                                                         @"hd_rectification_company_text":MNumber,
                                                         
                                                         @"hd_rectification_pic_one":self.hd_report_pic_one?self.hd_report_pic_one:@"",
                                                        @"hd_rectification_pic_two":self.hd_report_pic_two?self.hd_report_pic_two:@"",
                                                        @"hd_rectification_pic_three":self.hd_report_pic_three?self.hd_report_pic_three:@"",
                                                         @"hd_rectification_pic_audio": self.hd_rectification_pic_audio?self.hd_rectification_pic_audio:@"",
                                                         
                                                         @"hd_description_of_rectification":self.tv_note.text?self.tv_note.text:@"",
                                                         
            
        } WithCallBack:^(SQRectificationDetailsModel * data) {
            [CCToast showMessage:data.msg];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"rectificationDetails" object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }

    
}

#pragma mark ---- 验证
- (BOOL)verification {
    BOOL ret = NO;
   
    if([self.tv_note.text isEqualToString:@""]){
       
        [CCToast showMessage:@"请输入隐患描述"];
        return NO;
    }

     if (![self haveStr:@[self.hd_report_pic_one,self.hd_report_pic_two,self.hd_report_pic_three,self.hd_rectification_pic_audio]]) {
            
            [CCToast showMessage:@"请上传照片或者音频"];
            return NO ;
        
    }else{
        ret = YES;
    }
    return YES;
}



- (BOOL)haveStr:(NSArray<NSString *> *)strs{
    for (NSString *key in strs) {
        if (key.length > 0) {
            return YES;
        }
    }
    return NO;
}




//༥༤༽ཀབཀྲ་ཤིས་བདེ་ལེགས།

/**隐患整改
*/
- (IBAction)clickConfirm:(UIButton *)sender {
    
    [self.tableView beginUpdates];
    self.tableView.tableHeaderView.height = W(459);
    [self.tableView  endUpdates];
    
    [self.btn_confirm setTitleColor:[UIColor colorNamed:@"AccentColor"] forState:UIControlStateNormal];
    [self.btn_information setTitleColor:[UIColor colorNamed:@"333Color"] forState:UIControlStateNormal];
    self.view_confirm.hidden = NO;
    self.view_information.hidden = YES;
}


/**隐患信息*/
- (IBAction)clickInformation:(UIButton *)sender {
    [self.tableView beginUpdates];
    self.tableView.tableHeaderView.height = W(1062);
    [self.tableView  endUpdates];
    
    [self.btn_information setTitleColor:[UIColor colorNamed:@"AccentColor"] forState:UIControlStateNormal];
    [self.btn_confirm setTitleColor:[UIColor colorNamed:@"333Color"] forState:UIControlStateNormal];
    self.view_confirm.hidden = YES;
    self.view_information.hidden = NO;
    
}

#pragma mark -- 图片放大
- (IBAction)clickShowImage:(UIButton *)sender {

    if (sender.tag == 10) {
        if ([self haveStr:@[self.queryModel.data.hd_report_pic_one]]) {
            [SQShowImage showImage:self.img_picOne];
            
        }
        
    }else if (sender.tag == 20){
        if ([self haveStr:@[self.queryModel.data.hd_report_pic_two]]) {
            [SQShowImage showImage:self.img_picTwo];
        }
        
        
    }else if (sender.tag == 30){
        if ([self haveStr:@[self.queryModel.data.hd_report_pic_three]]) {
            [SQShowImage showImage:self.img_picThree];
        }
            
    }
}




@end
